// pages/reportSearch/reportSearch.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchContent:'',
    searchClear:false,
    searchList:[],
    hotList:[],
    historySearchOrg:[],
    reportOrg:[],
    orgid:'',
    id:'',
    canIpt:true,
    showone:null,
    showModal:false,
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  searchIpt(e) {
    var that = this
    if (!that.data.canIpt) {
      return
    }
    that.toggle('searchContent', e.detail.value)
    if (!e.detail.value) {
      that.toggle('searchClear', false)
    }
  },
  clickSearch(e) {
    var that = this;
    var dataset = e.currentTarget.dataset;
    that.toggle('canIpt', false)
    that.toggle('searchContent', dataset.key)
    that.getOrglist()
  },
  searchClear() {
    var that = this
    that.toggle('searchContent', '')
    that.toggle('searchClear', false)
    that.toggle('searchList', [])
  },
  clearHistory() {
    var that = this
    wx.showModal({
      title: '提示',
      content: '是否清空搜索记录？',
      cancelColor:'#999999',
      confirmColor:'#2168eb',
      success(res) {
        if (res.confirm) {
          that.toggle('historySearchOrg', [])
          wx.setStorage({
            key: "historySearchOrg",
            data: []
          })
        }
      }
    })
  },
  reportBtn(e){
    var that = this
    var id = e.currentTarget.dataset.id
    var type = e.currentTarget.dataset.type
    that.toggle('id', id)
    that.toggle('showone', type)
    that.toggle('showModal', true)
  },
  light(str) {
    var index = str.indexOf(this.data.searchContent),
      i = this.data.searchContent.length,
      f = str.substring(0, index),
      s = str.substring(index, i + index),
      t = str.substring(i + index, str.length),
      arr = [f, s, t];
    return arr
  },
  getOrglist(){
    var that = this
    if (that.data.searchContent.length) {
      that.toggle('searchClear', true)
    }
    app.req({
      url: "orgmini/rwOrgReport/queryForOrgName",
      data: {
        orgId: that.data.orgid,
        orgName: that.data.searchContent,
      },
      success: function (res) {
        if (200 == res.code) {
          var searchList = res.data
          for (var item of searchList) {
            item.orgName = that.light(item.orgName)
          }
          that.toggle("searchList", searchList);
        }
        wx.getStorage({
          key: 'historySearchOrg',
          success(res) {
            var historySearchOrg = res.data
            if (that.data.searchContent.length) {
              if (historySearchOrg.indexOf(that.data.searchContent) == -1) {
                historySearchOrg.unshift(that.data.searchContent)
                wx.setStorage({
                  key: "historySearchOrg",
                  data: historySearchOrg,
                  success(res) {
                    that.toggle('historySearchOrg', historySearchOrg)
                  }
                })
              } else {
                historySearchOrg.splice(historySearchOrg.indexOf(that.data.searchContent), 1);
                historySearchOrg.unshift(that.data.searchContent)
                wx.setStorage({
                  key: "historySearchOrg",
                  data: historySearchOrg,
                  success(res) {
                    that.toggle('historySearchOrg', historySearchOrg)
                  }
                })
              }
            }

          }
        })
        that.toggle('canIpt', true)
      }
    });
  },
  savereport(){
    var that = this
    app.req({
      url: "orgmini/rwOrgReport/save",
      data: {
        orgId: that.data.orgid,
        type: that.data.showone,
        parentOrg: that.data.id
      },
      success: function (res) {
        if (200 == res.code) {
          wx.showToast({
            title: '上报成功',
          })
          that.toggle('id', '')
          that.toggle('showone', null)
          that.toggle('showModal', false)
        }else{
          that.toggle('id', '')
          that.toggle('showone', null)
          that.toggle('showModal', false)
        }
      }
    });
  },
  cancel(){
    var that = this
    that.toggle('id', '')
    that.toggle('showone', null)
    that.toggle('showModal', false)
  },
  //正在上报
  getreportOrg(){
    var that = this;
    app.req({
      url: "orgmini/rwOrgReport/reportOrg",
      data: {
        orgId: that.data.orgid
      },
      success: function (res) {
        if (200 == res.code) {
          that.toggle("reportOrg", res.data);
        }
      }
    });
  },
  // 热搜榜
  hotList: function () {
    var that = this;
    app.req({
      url: "orgmini/org/recommendList",
      data: {
        "pageNumber": 1,
        "pageSize": 10
      },
      success: function (res) {
        if (200 == res.code) {
          var hotList = res.data.rows
          that.toggle("hotList", hotList);
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.toggle('orgid',options.id)
    that.hotList()
    that.getreportOrg()
    wx.getStorage({
      key: 'historySearchOrg',
      success(res) {
        that.toggle('historySearchOrg', res.data)
      },
      fail(res) {
        var arr = []
        wx.setStorage({
          key: "historySearchOrg",
          data: arr
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})